package com.huanzhidadi.springsecurity.model;


import lombok.Data;

import java.io.Serializable;

/**
 * @author gongxp
 * @description
 * @date 2020/7/7 09:27
 */
@Data
public class BaseResultVO<T> implements Serializable {
    private static final long serialVersionUID = 7045142366784960794L;

    private Integer status;
    private String message;
    private T data;

    public static final Integer SUCCESS_CODE = 200;
    public static final Integer ERROR_CODE = 500;
    public static final Integer NOT_LOGIN = 300;

    public static final String SUCCESS_MSG = "操作成功";
    public static final String ERROR_MSG = "操作失败";
    public static final String NOT_LOGIN_MSG = "未登录";
    public static final String NOT_POWER_MSG = "权限不足";

    public BaseResultVO(T data, int status, String message) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public BaseResultVO(T data) {
        this.data = data;
    }

    public BaseResultVO(int status, String message) {
        this.status = status;
        this.message = message;
    }

    public BaseResultVO() {
        status = SUCCESS_CODE;
        message = SUCCESS_MSG;
    }

    public boolean isSuccess() {
        return SUCCESS_CODE.equals(status);
    }

    /**
     * 成功返回
     */
    private static final BaseResultVO<?> SUCCESS_CACHE = new BaseResultVO<>(SUCCESS_CODE, SUCCESS_MSG);

    @SuppressWarnings("unchecked")
    public static <T> BaseResultVO<T> success() {
        return (BaseResultVO<T>) SUCCESS_CACHE;
    }

    public static <T> BaseResultVO<T> success(T data) {
        if (data instanceof String) {
            return new BaseResultVO<>(SUCCESS_CODE, data.toString());
        }
        return new BaseResultVO<>(data, SUCCESS_CODE, SUCCESS_MSG);
    }
    public static <T> BaseResultVO<T> success(String msg, T data) {
        return new BaseResultVO<>(data, SUCCESS_CODE, msg);
    }
    /**
     * 错误返回
     * @param message
     * @param <T>
     * @return
     */
    public static <T> BaseResultVO<T> error(String message) {
        return new BaseResultVO<>(ERROR_CODE, message);
    }

    public static <T> BaseResultVO<T> error() {
        BaseResultVO<T> baseResultVo = new BaseResultVO<>();
        baseResultVo.setStatus(BaseResultVO.ERROR_CODE);
        baseResultVo.setMessage(BaseResultVO.ERROR_MSG);
        return baseResultVo;
    }

    public static <T> BaseResultVO<T> error(Integer code, String message) {
        return new BaseResultVO<>(code, message);
    }

    public static <T> BaseResultVO<T> error(String msg, T data) {
        return new BaseResultVO<>(data, ERROR_CODE, msg);
    }

    public static BaseResultVO notPower() {
        return new BaseResultVO(NOT_LOGIN, NOT_POWER_MSG);
    }

    public static BaseResultVO notPowerError() {
        return new BaseResultVO(ERROR_CODE, NOT_POWER_MSG);
    }

    public static BaseResultVO notLogIn() {
        return new BaseResultVO(NOT_LOGIN, NOT_LOGIN_MSG);
    }


    @Override
    public String toString() {
        return "BaseResultVo{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
